package com.cy.personer.web;

import com.cy.personer.bean.Card;
import com.cy.personer.bean.User;
import com.cy.personer.biz.impl.CardBizImpl;
import com.cy.personer.biz.impl.UserBizImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/manageUser")
public class UserHandler {
    @Resource
    private UserBizImpl userBizImpl;
    @Resource
    private CardBizImpl cardBizImpl;

    //展现建卡页面
    @RequestMapping(value ="/showRegisterUser.action")
    public String showRegisterUser(){
        return "admin/addUser";
    }

    //检测该卡号是否可用
    @RequestMapping(value ="/cardNumberCheck.action")
    public @ResponseBody String cardNumberCheck (HttpServletRequest request,String cardNumber){
        String result = "error";
        Card card = cardBizImpl.checkCard(cardNumber);//检测该卡是否存在卡表里
        User user2 = userBizImpl.cardNumberCheck(cardNumber);//检测该卡是否已被使用了
        if (user2 != null){
            result = "success";
        }else if (card != null){
            result = "error";
        }else {
            result = "cardError";
        }

        return result;
    }


    //建卡页面
    @RequestMapping(value ="/registerUser.action")
    public @ResponseBody String registerUser (HttpServletRequest request,User user,String cardNumber){
        String result1 = "error";
        Card card = cardBizImpl.checkCard(cardNumber);
        if (card != null){
            user.setCardId(card.getCardId());
        }
        user.setStateId(1);
        boolean result = userBizImpl.addUser(user);
        if (result){
            result1 = "success";
        }
        return result1;
    }

}
